<template>
  <div>
    <BasicTable
      @register="registerTable"
      :rowSelection="{ type: 'checkbox', selectedRowKeys: checkedKeys, onChange: onSelectChange }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'material-symbols:delete-outline',
                tooltip: '删除',
                onClick: handleDel.bind(null, record),
              },
            ]"
        /></template>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="reload">更新模板库</a-button>
        <a-button :disabled="!checkedKeys.length" type="primary" @click="handleBatchDel"
          >批量删除</a-button
        >
      </template>
    </BasicTable>
    <PublishError @register="registerPublishError" @close="errorClose" />
  </div>
</template>

<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { deleteTemplate, templateList } from '@/api/version';
  import { getBasicColumns } from './tableData';
  import { Modal } from 'ant-design-vue';
  import { ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { ITemplateListResult } from '@/api/version/type';
  import { useModal } from '@/components/Modal';
  import PublishError from '../components/publishError.vue';

  const checkedKeys = ref<(string | number)[]>([]);
  const { createMessage } = useMessage();

  const [registerTable, { reload }] = useTable({
    title: '',
    api: templateList,
    columns: getBasicColumns(),
    rowKey: 'template_id',
    useSearchForm: false,
    searchInfo: {
      templateType: 0,
    },
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: true,
    clickToRowSelect: false,
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
    },
  });

  function onSelectChange(selectedRowKeys: (string | number)[]) {
    console.log('selectedRowKeys', selectedRowKeys);
    checkedKeys.value = selectedRowKeys;
  }

  const [registerPublishError, { openModal: openPublishErrorModal }] = useModal();

  async function requestDeleteTemplate(ids) {
    try {
      let res = await deleteTemplate({ ids });
      if (res && res.length) {
        return openPublishErrorModal(true, {
          list: res,
        });
      }
      reload();
      checkedKeys.value = [];
      createMessage.success('删除成功');
    } catch (error) {}
  }

  async function handleDel(row: ITemplateListResult) {
    Modal.confirm({
      title: '提示',
      content: '该操作将删除该模板',
      onOk: async () => {
        await requestDeleteTemplate([row.template_id]);
      },
    });
  }

  function handleBatchDel() {
    Modal.confirm({
      title: '提示',
      content: '该操作将批量删除所选模板',
      onOk: async () => {
        await requestDeleteTemplate(checkedKeys.value);
      },
    });
  }

  function errorClose() {
    reload();
    checkedKeys.value = [];
  }
</script>
